<?php
/**
 * 面向操作  基础模型  抽象工厂模型
 * 继承权限  这里是逻辑算法
 */
class News {
	
	public $_m; //文章模型 图片模型 专题模型等 包含了模板和内容
	public $_base; //基本模型
	public $_extend; //扩展模型
	protected $_data; //提交之后存放的参数
	protected $_cid; //栏目id
	protected $_mid; //模型id
	protected $_num = 40;
	
	public function __construct($cid, $mid) {
		//$this->_data = $_REQUEST; //初始化存储数据
		$this->_base = new BaseModule ();
		$this->_extend = new ExtendModule ();
		$this->_cid = isset ( $cid ) ? $cid : 2;
		$this->_mid = isset ( $mid ) ? $mid : 1;
		$this->_m = $this->load ( $this->_mid ); //配置选择器
	

	}
	
	//选择模型
	private function load($type) {
		switch ($type) {
			case 1 :
				return new ArticleModule ();
				break;
			case 2 :
				return new ImageModule ();
				break;
			case 3 :
				return new SpecialModule ();
				break;
		}
	}
	
	/**
	 * 汇总保存
	 */
	public function save($data, $id) {
		//获取模型
		$this->getConModule ( $data ['cid'] );
		//实例化基本信息
		$this->_base->save ( $data, $id );
		//实例化内容模型
		$this->_extend->save ( $data, $id );
		
		$this->_m->save ( $data, $id );
		return $this->_mid;
	}
	
	/**
	 * 一键模版设定
	 * Enter description here ...
	 * @param unknown_type $type
	 */
	public function temp($type) {
		return $this->_m->temp ( $type );
	}
	
	/**
	 * 初始化module
	 * Enter description here ...
	 */
	public function getConModule($cid) {
		$categoryInfo = $this->_base->getCategoryById ( $cid );
		$this->_mid = $categoryInfo ['module'];
		$this->_m = $this->load ( $this->_mid );
	}
	
	/**
	 * 检测文章是否存在
	 * @param int $id
	 * @return boolean true 不存在  false 存在
	 */
	private function checkIsExisit($id) {
		
		$rs = NewsModel::instance ()->getNewsWhere ( array ('id' => $id ) );
		return empty ( $rs );
	
	}
	/**
	 * 获取内容
	 * 增加一个检测机制 如果新闻不存在
	 * @param int $nid
	 */
	public function getCon($nid) {
		if ($this->checkIsExisit ( $nid )) {
			return;
		}
		
		$news = $this->_base->getCon ( $nid );
		
		$this->getConModule ( $news ['cid'] );
		$news ['mid'] = $this->_mid;
		
		//检测可能不存在mid值
		

		//检查内容模型
		$con = $this->_m->getCon ( $nid ); //内容
		

		$extend = $this->_extend->getCon ( $nid );
		
		//只有文章是一个
		if ($this->_mid == 1) {
			$news = $news + $con [0];
		}
		//二号
		if ($this->_mid == 2) {
			$news ['module'] = $con;
		}
		
		//三号 专题//这里到对专题内容再次解析
		if ($this->_mid == 3) {
			
			$news ['module'] = $this->parseSpecial ( $con );
		}
		
		$news ['extend'] = $extend; //将extend打入content中 形成一条
		

		//导入分组
		$categoryInfo = CategoryModel::instance ()->getCateogryById ( $news ['cid'] );
		$news ['category_name'] = $categoryInfo ['name'];
		
		if ($extend == NULL) {
			return array ('content' => $news, 'extend' => NULL );
		
		}
		// 重组 支持多个数组
		foreach ( $extend as $v ) {
			if (isset ( $news [$v ['key']] ) && is_array ( $news [$v ['key']] )) {
				array_push ( $news [$v ['key']], $v ['value'] );
			} elseif (isset ( $news [$v ['key']] ) && ! is_array ( $news [$v ['key']] )) {
				$news [$v ['key']] = array ($news [$v ['key']], $v ['value'] );
			} else {
				$news [$v ['key']] = $v ['value'];
			}
		}
		
		return array ('content' => $news, 'extend' => $extend );
	
	}
	/**
	 * 解析专题节点为文章或者图集
	 * @param Array $con
	 * @return Array
	 */
	private function parseSpecial($con) {
		
		foreach ( $con as $ck => $cv ) {
			$newsIds = explode ( ",", $cv ['newsid'] );
			foreach ( $newsIds as $v ) {
				if (! is_numeric ( $v )) {
					continue;
				}
				$s = $this->getCon ( $v );
				$con [$ck] ['list'] [] = $s ['content'];
				$s = "";
			}
		}
		return $con;
	}
	
	/**
	 * 这里应该传递参数 而不是直接赋值
	 * Enter description here ...
	 * @param unknown_type $cid
	 * @param unknown_type $p
	 * @param unknown_type $sort
	 * @param unknown_type $num
	 * @return false or rs  超出了分页数返回false
	 */
	public function listing($cid, $p, $sort, $num) {
		$cid = empty ( $cid ) ? 1 : $cid;
		$where = array ('cid' => $cid );
		$p = $p == 0 ? 1 : $p;
		// 默认设置为15条
		if ($num <= 0) {
			$newsConfig = require 'config/base.php';
			$num = isset ( $newsConfig ['news'] [$cid] ) ? $newsConfig ['news'] [$cid] : $newsConfig ['pagenum'];
		}
		
		/**按照什么排序*/
		$sort = $newsConfig ['sort'] ['cid'] != NULL ? $newsConfig ['sort'] ['cid'] : 'id';
		
		//指定分类页面按什么排序
		// 递归获取子类 并且过滤
		$ids = $this->_base->getSonCate ( $cid, $this->_mid );
		
		//进行缓存结果
		//先去找cache
		$totalNum = $this->getNewsNumCache ( $cid, $ids );
		
		$page = $this->page ( $totalNum, $p, $num );
		
		//超过了分页数
		if (! $page) {
			return false;
		}
		//获取基础数据
		$newslist = $this->_base->listing ( $page, $ids, $sort ); //获取基础数据
		

		return array ('newslist' => $newslist, 'totalnum' => $totalNum, 'page' => $page, 'module' => $this->_mid );
	
	}
	
	/**
	 * 换回统计缓存
	 * @param unknown_type $cid
	 */
	private function getNewsNumCache($cid, $ids) {
		
		$rs = CacheModel::instance ()->getCacheByCid ( array ('cid' => $cid ) );
		
		if (! empty ( $rs )) {
			return $rs ['value'];
		}
		
		$totalNum = $this->_base->getCountByWhere ( $ids, "IN" );
		CacheModel::instance ()->addCache ( array ('cid' => $cid, 'value' => $totalNum ) );
		return $totalNum;
	}
	
	/**
	 * 分页
	 *
	 * @return void
	 */
	private function page($total, $pageid, $num) {
		$pageid = isset ( $pageid ) ? $pageid : 1;
		$rs = $pageid * $num - $total;
		$start = ($pageid - 1) * $num;
		$pagenum = ceil ( $total / $num );
		/*修正分类不包含内容 显示404错误*/
		$pagenum = $pagenum == 0 ? 1 : $pagenum;
		/*如果超过了分类页数 404错误*/
		
		if ($pageid > $pagenum) {
			//echo FAILED . 'The paging number out of range';
			return false;
		
		//  exit();
		// header("HTTP/1.1 404 Not Found");
		}
		
		$page = array ('start' => $start, 'num' => $num, 'current' => $pageid, 'page' => $pagenum );
		return $page;
	}

}